﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WF_Business;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var sioTemp = new ChBusiness.FlowOperation.SerialInstanceOperation();
        var ida = SysParams.OAConnection();
        DataTable dtTemp;
        try
        {
            //var strSql = " select \"id\" as unitsid,(select \"id\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") relationunitsid,(select \"personname\" from xt_bmzmr_bak b where b.\"id\"=d.\"bm_id\") jbrname,(select \"idcard\" from xt_bmzmr_bak b where b.\"id\"=d.\"bm_id\") jbrcard,(select \"tel\" from xt_bmzmr_bak b where b.\"id\"=d.\"bm_id\") jbrtel,\"orgcode\",'1' orgtype,(select \"orgname\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") orgname,(select \"postcode\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") orgpostcode,(select \"address\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") orgaddress,(select \"tel\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") orgtel,(select \"email\" from xt_bmzm_bak b where b.\"id\"=d.\"bm_id\") orgemail,sys_guid() id,'' iid,'0' input_index from xt_dept_bak d where \"bm_id\" !='0' or \"zm_id\"!='0' ";
            var strSql = @" select id as unitsid,
                               (select id from xt_bmzm_bakNew b where b.id = d.bm_id) relationunitsid,
                               (select personname from xt_bmzmr_bakNew b where b.id = d.bm_id) jbrname,
                               (select idcard from xt_bmzmr_bakNew b where b.id = d.bm_id) jbrcard,
                               (select tel from xt_bmzmr_bakNew b where b.id = d.bm_id) jbrtel,
                               (select email from xt_bmzmr_bakNew b where b.id = d.bm_id) jbremail,
                               '' orgcode,
                               '1' orgtype,
                               (select orgname from xt_bmzm_bakNew b where b.id = d.bm_id) orgname,
                               (select postcode from xt_bmzm_bakNew b where b.id = d.bm_id) orgpostcode,
                               (select address from xt_bmzm_bakNew b where b.id = d.bm_id) orgaddress,
                               (select tel from xt_bmzm_bakNew b where b.id = d.bm_id) orgtel,
                               (select email from xt_bmzm_bakNew b where b.id = d.bm_id) orgemail,
                               sys_guid() id,
                               '' iid,
                               '0' input_index
                          from xt_dept_baknew d
                         where bm_id != '0'
                            or zm_id != '0' ";
            ida.RunSql(strSql, out dtTemp);
            dtTemp.Rows.Cast<DataRow>().ToList().ForEach(row =>
            {
                var iid = sioTemp.GetIIDWithEnterprise("", "", "").ToString();
                strSql = string.Format(@"insert into ut_unitsrelation20161121 (unitsid, relationunitsid, jbrname, jbrcard, jbrtel, orgcode, orgtype, orgname, orgpostcode, orgaddress, orgtel, orgemail, id, iid, input_index)
           values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','0')", row["unitsid"].ToString(),
                row["relationunitsid"].ToString(), row["jbrname"].ToString(), row["jbrcard"].ToString(), row["jbrtel"].ToString(), row["orgcode"].ToString(), row["orgtype"].ToString(), row["orgname"].ToString()
                , row["orgpostcode"].ToString(), row["orgaddress"].ToString(), row["orgtel"].ToString(), row["orgemail"].ToString(), Guid.NewGuid().ToString("N"), iid);
                ida.RunSql(strSql);
                iid = sioTemp.GetIIDWithEnterprise("", "", "").ToString();
                strSql = string.Format(@" insert into ut_jbrinfo20161121
                               (id, iid, input_index, jbrname, jbrcard, jbrtel, jbremail, createtime, unitsid)
                             values
                               ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', sysdate, '{7}')", Guid.NewGuid().ToString("N"), iid,"0",
                                row["jbrname"].ToString(), row["jbrcard"].ToString(), row["jbrtel"].ToString(), row["jbremail"].ToString(), row["unitsid"].ToString());
                ida.RunSql(strSql);
            });
            strSql = @" select id as unitsid,
                       (select id from xt_bmzm_bakNew b where b.id = d.zm_id) relationunitsid,
                       (select personname from xt_bmzmr_bakNew b where b.id = d.zm_id) jbrname,
                       (select idcard from xt_bmzmr_bakNew b where b.id = d.zm_id) jbrcard,
                       (select tel from xt_bmzmr_bakNew b where b.id = d.zm_id) jbrtel,
                       (select email from xt_bmzmr_bakNew b where b.id = d.zm_id) jbremail,
                       '' orgcode,
                       '2' orgtype,
                       (select orgname from xt_bmzm_bakNew b where b.id = d.zm_id) orgname,
                       (select postcode from xt_bmzm_bakNew b where b.id = d.zm_id) orgpostcode,
                       (select address from xt_bmzm_bakNew b where b.id = d.zm_id) orgaddress,
                       (select tel from xt_bmzm_bakNew b where b.id = d.zm_id) orgtel,
                       (select email from xt_bmzm_bakNew b where b.id = d.zm_id) orgemail,
                       sys_guid() id,
                       '' iid,
                       '0' input_index
                  from xt_dept_bakNew d
                 where (bm_id != '0'
                    or zm_id != '0') ";
            ida.RunSql(strSql, out dtTemp);
            dtTemp.Rows.Cast<DataRow>().ToList().ForEach(row =>
            {
                var iid = sioTemp.GetIIDWithEnterprise("", "", "").ToString();
                strSql = string.Format(@"insert into ut_unitsrelation20161121 (unitsid, relationunitsid, jbrname, jbrcard, jbrtel, orgcode, orgtype, orgname, orgpostcode, orgaddress, orgtel, orgemail, id, iid, input_index)
           values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','0')", row["unitsid"].ToString(),
                row["relationunitsid"].ToString(), row["jbrname"].ToString(), row["jbrcard"].ToString(), row["jbrtel"].ToString(), row["orgcode"].ToString(), row["orgtype"].ToString(), row["orgname"].ToString()
                , row["orgpostcode"].ToString(), row["orgaddress"].ToString(), row["orgtel"].ToString(), row["orgemail"].ToString(), Guid.NewGuid().ToString("N"), iid);
                ida.RunSql(strSql);
                strSql = string.Format(@" insert into ut_jbrinfo20161121
                               (id, iid, input_index, jbrname, jbrcard, jbrtel, jbremail, createtime, unitsid)
                             values
                               ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', sysdate, '{7}')", Guid.NewGuid().ToString("N"), iid, "0",
                                row["jbrname"].ToString(), row["jbrcard"].ToString(), row["jbrtel"].ToString(), row["jbremail"].ToString(), row["unitsid"].ToString());
                ida.RunSql(strSql);
            });
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }
}